package com.leo;

import com.leo.bean.Student;
import com.leo.simple.job.StudentJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @ClassName SimpleJobTest
 * @Description
 * @Author wangss
 * @date 2021.07.14 20:50
 * @Version 1.0
 */
public class SimpleJobTest {

    public static void main(String[] args) throws SchedulerException {
        // 创建schedule实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("scheduler", "schedulerValue");

        // 创建Trigger
        Trigger t1 = TriggerBuilder.newTrigger().withIdentity("t1", "g1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .build();

        // 创建从什么时刻开始执行的Trigger，到什么时刻结束的Trigger
        Trigger t2 = TriggerBuilder.newTrigger().withIdentity("t2", "g1").startAt(DateBuilder.todayAt(21, 00, 00))
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).endAt(DateBuilder.todayAt(21, 05, 00))
                .build();

        // 创建JOb实例
        JobDetail j1 = JobBuilder.newJob(StudentJob.class).withIdentity("j1", "g1").build();
        Student student = new Student("wangss", 15);
        j1.getJobDataMap().put("student", student);
        JobDetail j2 = JobBuilder.newJob(StudentJob.class).withIdentity("j2", "g1").build();
        Student student2 = new Student("ztt", 14);
        j2.getJobDataMap().put("student", student2);

        // 加入scheduler
        scheduler.scheduleJob(j1, t1);
        scheduler.scheduleJob(j2, t2);

        scheduler.start();
    }
}
